package com.njust.RateMyTray_backend.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 全局异常处理
 */
@ControllerAdvice(annotations = {RestController.class, Controller.class}) // 全局异常处理，只对 RestController 和 Controller 注解的类进行处理
@ResponseBody //返回json数据
@Slf4j //日志
public class GlobalExceptionHandler {

    /**
     * 异常处理方法
     * 处理 SQL 约束冲突异常
     */
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public Result<String> exceptionHandler(SQLIntegrityConstraintViolationException ex){
        log.error(ex.getMessage());

        if(ex.getMessage().contains("Duplicate entry")){
            String[] split = ex.getMessage().split(" ");
            String msg = split[2] + "已存在";
            return Result.error(400, msg);
        }

        return Result.error(500, "未知错误");
    }

    /**
     * 异常处理方法
     * 处理自定义异常
     */
    @ExceptionHandler(CustomException.class)
    public ResponseEntity<Result<String>> exceptionHandler(CustomException ex){
        log.error(ex.getMessage());

        Result<String> result = Result.error(400, ex.getMessage());
        return new ResponseEntity<>(result, HttpStatus.BAD_REQUEST);
    }

    /**
     * 异常处理方法
     * 处理认证失败异常
     */
    @ExceptionHandler(AuthenticationException.class)
    public ResponseEntity<Result<String>> exceptionHandler(AuthenticationException ex) {
        log.warn("认证失败: {}", ex.getMessage());

        Result<String> result = Result.error(401, ex.getMessage());
        return new ResponseEntity<>(result, HttpStatus.UNAUTHORIZED);
    }

    /**
     * 异常处理方法
     * 处理资源未找到异常
     */
    @ExceptionHandler(ResourceNotFoundException.class)
    public ResponseEntity<Result<String>> exceptionHandler(ResourceNotFoundException ex) {
        log.warn("资源未找到: {}", ex.getMessage());

        Result<String> result = Result.error(404, ex.getMessage());
        return new ResponseEntity<>(result, HttpStatus.NOT_FOUND);
    }

    /**
     * 异常处理方法
     * 处理密码错误异常
     */
    @ExceptionHandler(InvalidPasswordException.class)
    public ResponseEntity<Result<String>> exceptionHandler(InvalidPasswordException ex) {
        log.warn("密码错误: {}", ex.getMessage());

        Result<String> result = Result.error(400, ex.getMessage());
        return new ResponseEntity<>(result, HttpStatus.BAD_REQUEST);
    }

    /**
     * 异常处理方法
     * 处理权限不足异常
     */
    @ExceptionHandler(ForbiddenException.class)
    public ResponseEntity<Result<String>> exceptionHandler(ForbiddenException ex) {
        log.warn("权限不足: {}", ex.getMessage());

        Result<String> result = Result.error(403, ex.getMessage());
        return new ResponseEntity<>(result, HttpStatus.FORBIDDEN);
    }
} 